import { create } from 'zustand'
import { immer } from 'zustand/middleware/immer'
import { ModelChat } from '@shared/types/entity'

interface ModelChatStore {
  data: ModelChat[]
  getData: () => Promise<void>
  form: {
    type: 'add' | 'edit'
    data?: ModelChat
  }
  editForm: (type: 'add' | 'edit', data?: ModelChat) => void
}

// 显式标注类型
const useModelChatStore = create<ModelChatStore>()(
  immer(
    (set): ModelChatStore => ({
      data: [],
      getData: async () => {
        const result = await window.api.modelChat.getList({ type: 'chat' })
        set({ data: result })
      },
      form: { type: 'add' },
      editForm: (type, data?) =>
        set((state) => {
          state.form.type = type
          state.form.data = data || {
            mark: '',
            name: '',
            url: '',
            model: ''
          }
        })
    })
  )
)

export default useModelChatStore
